<%-- 
    Document   : insertbook
    Created on : Sep 19, 2011, 9:57:52 AM
    Author     : Quangtv
--%>

<%@page import="Assignment.BLO.BookBLO"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Insert book</title>
        <link href="css/style.css" rel="stylesheet" type="text/css"/>
        <link href="css/bannerstyle.css" rel="stylesheet" type="text/css"/>
        <link href="css/TableCSS.css" rel="stylesheet" type="text/css"/>

        <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />

        <script type="text/javascript">
            function validate(element){
                var regExp = null;
                switch (element){
                    case document.getElementById("bookid"):
                        if(element.value.toString().length <= 20 && element.value.toString().length > 0){
                            document.getElementById("bookid_error").style.visibility = "hidden";
                            document.getElementById("bookid").style.borderColor = "#333333";
                            document.getElementById("registerbutton").disabled = false;
                            return true;
                        }else{
                            document.getElementById("bookid_error").style.visibility = "visible";
                            document.getElementById("bookid_error").textContent = "Must be filled & less than 20 characters";
                            document.getElementById("bookid").style.borderColor = "red";
                            document.getElementById("registerbutton").disabled = true;
                            return false;
                        }
                        break;
                    case document.getElementById("title"):
                        if(element.value.toString().length <= 50 && element.value.toString().length > 0){
                            document.getElementById("title_error").style.visibility = "hidden";
                            document.getElementById("registerbutton").disabled = false;
                            document.getElementById("title").style.borderColor = "#333333";
                            return true;
                        }else{
                            document.getElementById("title_error").style.visibility = "visible";
                            document.getElementById("title").style.borderColor = "red";
                            document.getElementById("registerbutton").disabled = true;
                            return false;
                        }
                        break;
                    case document.getElementById("author"):
                        if(element.value.toString().length >0 && element.value.toString().length <=50){
                            document.getElementById("author_error").style.visibility = "hidden";
                            document.getElementById("author").style.borderColor = "#333333";
                            document.getElementById("registerbutton").disabled = false;
                            return true;
                        }else{
                            document.getElementById("author_error").style.visibility = "visible";
                            document.getElementById("author").style.borderColor = "red";
                            document.getElementById("registerbutton").disabled = true;
                            return false;
                        }
                        break;
                    case document.getElementById("publisher"):
                        if(element.value.toString().length <= 50 && element.value.toString().length > 0){
                            document.getElementById("publisher_error").style.visibility = "hidden";
                            document.getElementById("registerbutton").disabled = false;
                            document.getElementById("publisher").style.borderColor = "#333333";
                            return true;
                        }else{
                            document.getElementById("publisher_error").style.visibility = "visible";
                            document.getElementById("publisher").style.borderColor = "red";
                            document.getElementById("registerbutton").disabled = true;
                            return false;
                        }
                        break;
                    case document.getElementById("category"):
                        if(element.value.toString().length <=50 && element.value.toString().length > 0){
                            document.getElementById("category_error").style.visibility = "hidden";
                            document.getElementById("registerbutton").disabled = false;
                            document.getElementById("category").style.borderColor = "#333333";
                            return true;
                        }else{
                            document.getElementById("category_error").style.visibility = "visible";                            
                            document.getElementById("category").style.borderColor = "red";
                            document.getElementById("registerbutton").disabled = true;
                            return false;
                        }
                        break;
                    case document.getElementById("description"):
                        if(element.value.toString().length <= 500 && element.value.toString().length > 0){
                            document.getElementById("description_error").style.visibility = "hidden";
                            document.getElementById("description").style.borderColor = "#333333";
                            document.getElementById("registerbutton").disabled = false;
                            return true;
                        }else{
                            document.getElementById("description_error").style.visibility = "visible";
                            //document.getElementById("email").focus();
                            document.getElementById("description").style.borderColor = "red";
                            document.getElementById("registerbutton").disabled = true;
                            return false;
                        }
                        break;
                    case document.getElementById("image"):
                        if(element.value.toString().length <= 500 && element.value.toString().length > 0){
                            document.getElementById("image_error").style.visibility = "hidden";
                            document.getElementById("image").style.borderColor = "#333333";
                            document.getElementById("registerbutton").disabled = false;
                            return true;
                        }else{
                            document.getElementById("image_error").style.visibility = "visible";
                            //document.getElementById("address").focus();
                            document.getElementById("image").style.borderColor = "red";
                            document.getElementById("registerbutton").disabled = true;
                            return false;
                        }
                        break;
                    case document.getElementById("price"):
                        regExp = /^([0-9]*|\d*\.\d{1}?\d*)$/;
                        if(element.value.match(regExp) && element.value.toString().length > 0){
                            document.getElementById("price_error").style.visibility = "hidden";
                            document.getElementById("price").style.borderColor = "#333333";
                            document.getElementById("registerbutton").disabled = false;
                            return true;
                        }else{
                            document.getElementById("price_error").style.visibility = "visible";
                            //document.getElementById("city").focus();
                            document.getElementById("price").style.borderColor = "red";
                            document.getElementById("registerbutton").disabled = true;
                            return false;
                        }
                        break;
                    case document.getElementById("inventory"):
                        regExp = /^[0-9]+$/;
                        if(element.value.match(regExp) && element.value.toString().length > 0){
                            document.getElementById("inventory_error").style.visibility = "hidden";
                            document.getElementById("inventory").style.borderColor = "#333333";
                            document.getElementById("registerbutton").disabled = false;
                            return true;
                        }else{
                            document.getElementById("inventory_error").style.visibility = "visible";
                            document.getElementById("inventory").style.borderColor = "red";
                            document.getElementById("registerbutton").disabled = true;
                            return false;
                        }
                }
            }
            function checkNULL(){
                if(document.getElementById("bookid").value.toString().length == 0){
                    document.getElementById("bookid_error").textContent = "Must be filled & less than 50 characters";
                    document.getElementById("bookid").style.borderColor = "red";
                    document.getElementById("bookid_error").style.visibility = "visible";
                    document.getElementById("title_error").style.visibility = "collapse";
                    document.getElementById("author_error").style.visibility = "collapse";
                    document.getElementById("publisher_error").style.visibility = "collapse";
                    document.getElementById("category_error").style.visibility = "collapse";
                    document.getElementById("description_error").style.visibility = "collapse";
                    document.getElementById("image_error").style.visibility = "collapse";
                    document.getElementById("price_error").style.visibility = "collapse";
                    document.getElementById("inventory_error").style.visibility = "collapse";
                    document.getElementById("bookid").focus();
                    return false;
                } else if(document.getElementById("title").value.toString().length == 0){
                    document.getElementById("title").style.borderColor = "red";
                    document.getElementById("bookid_error").style.visibility = "collapse";
                    document.getElementById("title_error").style.visibility = "visible";
                    document.getElementById("author_error").style.visibility = "collapse";
                    document.getElementById("publisher_error").style.visibility = "collapse";
                    document.getElementById("category_error").style.visibility = "collapse";
                    document.getElementById("description_error").style.visibility = "collapse";
                    document.getElementById("image_error").style.visibility = "collapse";
                    document.getElementById("price_error").style.visibility = "collapse";
                    document.getElementById("inventory_error").style.visibility = "collapse";
                    document.getElementById("title").focus();
                    return false;
                }else if (document.getElementById("author").value.toString().length == 0){
                    document.getElementById("author").style.borderColor = "red";
                    document.getElementById("bookid_error").style.visibility = "collapse";
                    document.getElementById("title_error").style.visibility = "collapse";
                    document.getElementById("author_error").style.visibility = "visible";
                    document.getElementById("publisher_error").style.visibility = "collapse";
                    document.getElementById("category_error").style.visibility = "collapse";
                    document.getElementById("description_error").style.visibility = "collapse";
                    document.getElementById("image_error").style.visibility = "collapse";
                    document.getElementById("price_error").style.visibility = "collapse";
                    document.getElementById("inventory_error").style.visibility = "collapse";
                    document.getElementById("author").focus();
                    return false;
                } else if (document.getElementById("publisher").value.toString().length == 0){
                    document.getElementById("publisher").style.borderColor = "red";
                    document.getElementById("bookid_error").style.visibility = "collapse";
                    document.getElementById("title_error").style.visibility = "collapse";
                    document.getElementById("author_error").style.visibility = "collapse";
                    document.getElementById("publisher_error").style.visibility = "visible";
                    document.getElementById("category_error").style.visibility = "collapse";
                    document.getElementById("description_error").style.visibility = "collapse";
                    document.getElementById("image_error").style.visibility = "collapse";
                    document.getElementById("price_error").style.visibility = "collapse";
                    document.getElementById("inventory_error").style.visibility = "collapse";
                    document.getElementById("publisher").focus();
                    return false;
                } else if (document.getElementById("category").value.toString().length == 0){
                    document.getElementById("category").style.borderColor = "red";
                    document.getElementById("bookid_error").style.visibility = "collapse";
                    document.getElementById("title_error").style.visibility = "collapse";
                    document.getElementById("author_error").style.visibility = "collapse";
                    document.getElementById("publisher_error").style.visibility = "collapse";
                    document.getElementById("category_error").style.visibility = "visible";
                    document.getElementById("description_error").style.visibility = "collapse";
                    document.getElementById("image_error").style.visibility = "collapse";
                    document.getElementById("price_error").style.visibility = "collapse";
                    document.getElementById("inventory_error").style.visibility = "collapse";
                    document.getElementById("category").focus();
                    return false;
                } else if (document.getElementById("description").value.toString().length == 0){
                    document.getElementById("description").style.borderColor = "red";
                    document.getElementById("bookid_error").style.visibility = "collapse";
                    document.getElementById("title_error").style.visibility = "collapse";
                    document.getElementById("author_error").style.visibility = "collapse";
                    document.getElementById("publisher_error").style.visibility = "collapse";
                    document.getElementById("category_error").style.visibility = "collapse";
                    document.getElementById("description_error").style.visibility = "visible";
                    document.getElementById("image_error").style.visibility = "collapse";
                    document.getElementById("price_error").style.visibility = "collapse";
                    document.getElementById("inventory_error").style.visibility = "collapse";
                    document.getElementById("description").focus();
                    return false;
                } else if (document.getElementById("image").value.toString().length == 0){
                    document.getElementById("image").style.borderColor = "red";
                    document.getElementById("bookid_error").style.visibility = "collapse";
                    document.getElementById("title_error").style.visibility = "collapse";
                    document.getElementById("author_error").style.visibility = "collapse";
                    document.getElementById("publisher_error").style.visibility = "collapse";
                    document.getElementById("category_error").style.visibility = "collapse";
                    document.getElementById("description_error").style.visibility = "collapse";
                    document.getElementById("image_error").style.visibility = "visible";
                    document.getElementById("price_error").style.visibility = "collapse";
                    document.getElementById("inventory_error").style.visibility = "collapse";
                    document.getElementById("image").focus();
                    return false;
                } else if (document.getElementById("price").value.toString().length == 0){
                    document.getElementById("price").style.borderColor = "red";
                    document.getElementById("bookid_error").style.visibility = "collapse";
                    document.getElementById("title_error").style.visibility = "collapse";
                    document.getElementById("author_error").style.visibility = "collapse";
                    document.getElementById("publisher_error").style.visibility = "collapse";
                    document.getElementById("category_error").style.visibility = "collapse";
                    document.getElementById("description_error").style.visibility = "collapse";
                    document.getElementById("image_error").style.visibility = "collapse";
                    document.getElementById("price_error").style.visibility = "visible";
                    document.getElementById("inventory_error").style.visibility = "collapse";
                    document.getElementById("price").focus();
                    return false;
                } else if (document.getElementById("inventory").value.toString().length == 0){
                    document.getElementById("price").style.borderColor = "red";
                    document.getElementById("bookid_error").style.visibility = "collapse";
                    document.getElementById("title_error").style.visibility = "collapse";
                    document.getElementById("author_error").style.visibility = "collapse";
                    document.getElementById("publisher_error").style.visibility = "collapse";
                    document.getElementById("category_error").style.visibility = "collapse";
                    document.getElementById("description_error").style.visibility = "collapse";
                    document.getElementById("image_error").style.visibility = "collapse";
                    document.getElementById("price_error").style.visibility = "collapse";
                    document.getElementById("inventory_error").style.visibility = "visible";
                    document.getElementById("inventory").focus();
                    return false;
                } else{
                    return true;
                }
            }

            function checkSearch(){
                if (document.getElementById("searchText").value.toString().length == 0){
                    alert("Must input somrthing!")
                    return false;
                }else{
                    return true;
                }
            }

            function checkExitsBook(isExits){
                if(isExits == "true"){
                    document.getElementById("bookid_error").style.visibility = "visible";
                    document.getElementById("bookid_error").textContent = "BookID is already exits!";

                }else{
                    document.getElementById("bookid_error").style.visibility = "collapse";
                }
            }

            function setVisibility(showUser, isAdmin){
                if(showUser == "true"){
                    document.getElementById("user").style.visibility = "visible";
                    document.getElementById("loggeduser").style.visibility = "hidden";
                }else if(showUser == "false" && isAdmin == "true"){
                    document.getElementById("loggeduser").style.visibility = "visible";
                    document.getElementById("user").style.visibility = "hidden";
                    document.getElementById("user_link").textContent = "| Admin";
                    document.getElementById("user_link").href = "admin.jsp";
                }else if(showUser == "false" && isAdmin == "false"){
                    document.getElementById("loggeduser").style.visibility = "visible";
                    document.getElementById("user").style.visibility = "hidden";
                    document.getElementById("user_link").textContent = "| View Cart";
                    document.getElementById("user_link").href = "viewcart.jsp";
                }
            }
        </script>
        <%
                    String loggedUser = (String) session.getAttribute("USER");
                    String showUser = "";
                    String isAdmin = "";
                    if (loggedUser == null) {
                        showUser = "true";
                    } else {
                        showUser = "false";
                        isAdmin = (String) session.getAttribute("ISADMIN");
                        if (isAdmin == null || isAdmin == "false") {
                            isAdmin = "false";
                        } else {
                            isAdmin = "true";
                        }
                    }
                    BookBLO bBLO = new BookBLO();
                    String bookID = bBLO.generateBookID();
        %>
    </head>
    <body onload='setVisibility("<%=showUser%>", "<%=isAdmin%>")'>
        <div id="header-wrap">
            <div id="header">
                <div style="margin:0px; text-align: left; float: left;"> <span style="color: #FFFFFF; font-family:'Arial Black',Gadget,sans-serif; font-size: 20px; font-weight: bold; letter-spacing: -2px;"><a href="index.jsp" style="color: #FFF; text-decoration: none;" title="Awesomebooks.com">AWESOMEBOOKS</a></span> <a style="color: #FFF; font-size: 18px; font-family: Georgia, 'Times New Roman', Times, serif; font-style:italic; font-weight:normal; text-decoration: none;" href="index.jsp" title="Find your own book">Find your own book</a> 	</div>
                <ul class="menu">

                    <!-- Begin Simple Item Without Drop -->
                    <li class="">
                        <a href="newrelease.jsp">New Release</a>
                    </li>
                    <li class="">
                        <a href="bestseller.jsp">Best Seller</a>
                    </li>
                    <li class="">
                        <a href="bookreview.jsp">Books Review</a>
                    </li>

                    <li class="drop">
                        <a href="#">Catalogs</a>
                        <span class="toggle">&nbsp;</span>
                        <ul>
                            <li><a href="Controller?Action=Search&searchText=literature&Category=true">Literature</a></li>
                            <li><a href="Controller?Action=Search&searchText=life style&Category=true">Life Style</a></li>
                            <li><a href="Controller?Action=Search&searchText=tutorial&Category=true">Tutorial</a></li>
                            <li><a href="Controller?Action=Search&searchText=discovery&Category=true">Discovery</a></li>
                            <li><a href="Controller?Action=Search&searchText=science&Category=true">Science</a></li>
                            <li><a href="Controller?Action=Search&searchText=kids&Category=true">Kids</a></li>
                        </ul>
                    </li>
                </ul>
            </div>
            <div id="underheader">
                <form action="Controller" onsubmit="return checkSearch()" method="POST">
                    <span id="searchbox" style="width:50%">
                        <input id="searchText" name="searchText" type="text" style="width:250px; border:1px solid #333333" />
                        <input name="Action" type="submit" value="Search" id="button" />
                    </span>
                    <span id="user" style="width:50%">
                        <a href="login.jsp">Login</a> |
                        <a href="register.jsp">Register</a>
                    </span>
                    <span id="loggeduser" style="width:50%;position: relative; left: -180px; visibility: hidden">
                        <a><%=loggedUser%></a>
                        <a href="Controller?Action=Logout">(Logout)</a>
                        <a href="viewcart.jsp" id="user_link">| View Cart</a>
                    </span>
                </form>
            </div>
        </div>
        <center>
            <br/>
            <div id="registerForm">
                <form action="Controller" enctype="multipart/form-data" method="post">
                    <fieldset>
                        <legend>Book Details</legend>
                        <div><label>BookID</label><input name="bookid" id="bookid" type="text" value="<%=bookID%>" style="border-color: #333333; background-color: gray" readonly="true" onblur="validate(document.getElementById('bookid'))"/></div>
                        <div><label>Title</label><input name="title" id="title" type="text" style="border-color: #333333" onblur="validate(document.getElementById('title'))"/></div>
                        <div><label>Author</label><input name="author" id="author" type="text" style="border-color: #333333" onblur="validate(document.getElementById('author'))"/></div>
                        <div><label>Publisher</label><input name="publisher" id="publisher" type="text"  style="border-color: #333333" onblur="validate(document.getElementById('publisher'))"/></div>
                        <div><label>Category</label>
                            <!--<input name="category" id="category" type="text"  style="border-color: #333333" onblur="validate(document.getElementById('category'))"/>-->
                            <select name="category" id="category" style="width: 171px; position: relative; left: 5px; border: 1px #333333 solid">
                                <option value="Literature">Literature</option>
                                <option value="Life Style">Life Style</option>
                                <option value="Tutorial">Tutorial</option>
                                <option value="Discovery">Discovery</option>
                                <option value="Science">Science</option>
                                <option value="Kids">Kids</option>
                            </select>
                        </div>
                        <div><label>Description</label><textarea cols="20" rows="3" name="description" type="text" id="description" style="border: 1px #333333 solid; color: #000000; font-size: 14px; position: relative; left: 5px;" onblur="validate(document.getElementById('description'))"></textarea></div>
                        <div><label>Cover</label><input name="image" id="image" type="file" size="11" onblur="validate(document.getElementById('image'))"/></div>
                        <div><label>Price</label><input name="price" id="price" type="text"  style="border-color: #333333; color: #000000; font-size: 14px" onblur="validate(document.getElementById('price'))"/></div>
                        <div><label>Quantity</label><input name="inventory" id="inventory" type="text"  style="border-color: #333333; color: #000000; font-size: 14px" onblur="validate(document.getElementById('inventory'))"/></div>
                        <div><label>Best Seller</label><input type="checkbox" name="bestseller" value="ON" /></div>
                    </fieldset>
                    <div id="errorcheck2">
                        <label id="bookid_error" style="color: red; visibility: collapse;width: 300px"></label><br/>
                        <label id="title_error" style="color: red; visibility: collapse; width: 300px">Must be filled & less than 50 characters</label><br/>
                        <label id="author_error" style="color: red; visibility: collapse; width: 300px">Must be filled & less than 50 characters</label>
                        <label id="publisher_error" style="color: red;visibility: collapse; width: 300px">Must be filled & less than 50 characters</label>
                        <label id="category_error" style="color: red; visibility: collapse; width: 300px">Must be filled & less than 50 characters</label>
                        <label id="description_error" style="color: red; visibility: collapse; width: 300px">Must be less than 500 characters</label>
                        <label id="image_error" style="color: red; visibility: collapse; width: 300px">Must choose a file to upload</label>
                        <label id="price_error" style="color: red; visibility: collapse; width: 300px">Must be numbers</label>
                        <label id="inventory_error" style="color: red; visibility: collapse; width: 300px">Must be numbers</label>
                    </div>

                    <button id ="registerbutton" name="Action" value="InsertBook" type="submit" onclick="return checkNULL()" style="position: relative; top: -355px">Insert</button>
                    <a href="admin.jsp"><button id ="registerbutton" type="button" style="position: relative; top: -355px">Cancel</button></a>
                </form>
            </div>
        </center>
        <br/>
    </body>
</html>
